package com.example.mqproduct.controller;


import com.example.mqproduct.entity.BikebaseinfoEntity;
import com.example.mqproduct.mapper.BikeMapper;
import com.rabbitmq.client.AMQP;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessageDeliveryMode;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

/**
 * @Author : JCccc
 * @CreateTime : 2019/9/3
 * @Description :
 **/
@RestController
public class SendMessageController {
 
    @Autowired
    RabbitTemplate rabbitTemplate;  //使用RabbitTemplate,这提供了接收/发送等等方法
    @Autowired
    BikeMapper bikeMapper;


//    @Scheduled(cron = "*/30 * * * * ? ")
    @GetMapping("/sendDirectMessage")
    public String sendDirectMessage() {
        String messageId = String.valueOf(UUID.randomUUID());
//        String messageData = msg;
        String createTime = LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
        Map<String,Object> map=new HashMap<>();
        map.put("messageId",messageId);
//        map.put("messageData",messageData);
        map.put("createTime",createTime);
        List<BikebaseinfoEntity> bikebaseinfoEntities = bikeMapper.list();
        List<String> list = bikeMapper.getRealtion("bikebaseinfo");
        //将消息携带绑定键值：TestDirectRouting 发送到交换机TestDirectExchange
        for (String bikebaseinfoEntity : list) {
            rabbitTemplate.convertAndSend("TestDirectExchange", "TestDirectRouting", bikebaseinfoEntity);
        }

        return "ok";
    }


}